import Vue from "vue";
import Router from "vue-router";
import Login from "@/views/Login";
import Error from "@/views/Error";
import store from './../store'

import Main from "@/views/Main/Main";
import Redirect from "@/views/Redirect/Redirect";
import AuthCenter from "@/views/AuthCenter/AuthCenter";


import axios from '@/utils/http'
import {Message} from "element-ui";
import NpmsHome from "@/views/NpmsHome/NpmsHome";
import Dashboard from "@/views/NPMS/Dashboard/Dashboard";

import Handover from "@/views/NPMS/Customer/Handover";


import Report from "@/views/NPMS/Report/Report";
import SearchCusRelation from "@/views/NPMS/Report/SearchCusRelation";
import SearchHandover from "@/views/NPMS/Report/SearchHandover";
import SearchReport from "@/views/NPMS/Report/SearchReport";
import Customer from "@/views/NPMS/Customer/CustomerManage/Customer";
import Chance from "@/views/NPMS/Chance/ChanceManage/Chance";
import Approval from "@/views/NPMS/Chance/ChanceApproval/Approval";

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: "/main",
            name: "Main",
            component: Main,
        },
        {
            path: "/",
            name: 'main',
            redirect: '/main',
        },
        {
            path: "/redirect",
            name: "redirect",
            component: Redirect,
        },
        {
            path: "/pass",
            name: "Pass",
            component: AuthCenter,
        },
        {
            path: '/login',
            name: 'login',
            component: Login,
        },
        {
            path: '/npms',
            name: 'NpmsHome',
            redirect: '/npms/customer',
            component: NpmsHome,
            children: [
                {
                    path: '/npms/dashboard',
                    name: 'Dashboard',
                    component: Dashboard,
                },
                {
                    path: '/npms/customer',
                    name: 'Customer',
                    component: Customer,
                },
                {
                    path: '/npms/handover',
                    name: 'Handover',
                    component: Handover,
                },
                {
                    path: '/npms/approval',
                    name: 'Approval',
                    component: Approval,
                },
                {
                    path: '/npms/chance',
                    name: 'Chance',
                    component: Chance,
                },
                {
                    path: '/npms/report',
                    name: 'Report',
                    component: Report,
                },
                {
                    path: '/npms/searchCusRelation',
                    name: 'SearchCusRelation',
                    component: SearchCusRelation,
                },
                {
                    path: '/npms/searchHandover',
                    name: 'SearchHandover',
                    component: SearchHandover,
                },
                {
                    path: '/npms/searchReport',
                    name: 'SearchReport',
                    component: SearchReport,
                }
            ]
        },
        {
            path: '/*',
            name: 'error',
            component: Error,
        }
    ]
})

router.beforeEach((to, from, next) => {
    //配置无token可以访问的页面
    if (to.name === 'redirect' || to.name === 'Main') {
        next()
        return;
    }

    if (to.name === 'Pass')
        next()
    const token = localStorage.getItem("token")
    if (token) {
        axios.get("/user/currentUser",).then(e => {
            if (e.data.status === 200) {
                //存到store里面
                console.log(e)
                var user = e.data.data
                store.dispatch("setUser", user)
                store.dispatch("setLogin", true)

                // 验证token是有效的
                //如果用户重复访问认证中心将重定向到首页
                if (to.name === 'Pass') {
                    next({
                        name: "redirect",
                        query: {
                            info: "You have logged in. Going to the home page now",
                        }
                    })
                } else {
                    next()
                }
            }
        }).catch(e => {
            store.dispatch("setUser", null)
            store.dispatch("setLogin", false)
            if (to.name === 'Pass') {
                Message.error("ID information invalid, please login again!")
                next()
            } else {
                next({
                    name: "redirect",
                    query: {
                        info: "Going to the Uniform Certification Center now",
                        to: "/pass",
                    }
                })
            }
        })
    } else {
        if (to.name === 'Pass') {
            next()
            return;
        }
        //没有token且访问需要token的页面
        //前往重定向页面引导至登录窗口
        var tt = to.fullPath
        console.log(tt)
        next({
            name: "redirect",
            query: {
                info: "Going to the Uniform Certification Center now",
                to: "/pass",
                redirect: tt
            }
        })

    }
})


export default router;